﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Hospitals.SystemManage
{
    public partial class CompanyList : BasePage
    {

        IBLL_Company ic = Factory.BLL_Company();

        IBLL_SelectRelation iselectRelation = Factory.BLL_SelectRelation();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCompanyInfo();
                
            }
        }
        /// <summary>
        /// 获取未审核的企业
        /// </summary>
        private void BindCompanyInfo()
        {
            //从页面获取分页大小
            int totalCount;
            AspNetPager1.PageSize = 10;
            string strWhere = "";

           
            if (string.IsNullOrEmpty(txtCompanyName.Text.Trim()) == false && !string.IsNullOrEmpty(HZ.Fun2.CheckString(txtCompanyName.Text.Trim())))
            {
                strWhere += " and TB_CompanyInfo.companyname like '%" + txtCompanyName.Text.Trim() + "%' ";
            }
            if (string.IsNullOrEmpty(txtCompanyid.Text.Trim()) == false && !string.IsNullOrEmpty(HZ.Fun2.CheckString(txtCompanyid.Text.Trim())))
            {
                strWhere += " and TB_CompanyInfo.companyid like '%" + txtCompanyid.Text.Trim() + "%' ";
            }
            if (ddltype.SelectedValue != "")
            {
                strWhere += " and TB_Company.companytype = '" + ddltype.SelectedValue + "'";
            }
            strWhere += "  order by TB_CompanyInfo.companyname";
            DataTable dt = ic.GetCompanyInfoByWhere(strWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwCheckList.DataSource = dt;
            gvwCheckList.DataBind();

            this.gvwCheckList.Height = this.gvwCheckList.Rows.Count * 30 + 50;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BindCompanyInfo();

        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindCompanyInfo();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected string getCheckstatus(string str)
        {
            if (str == "1")
            {
                return "已通过";
            }
            if (str == "2")
            {
                return "未通过";
            }
            if (str == "0")
            {
                return "未审核";
            }
            return "";
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hzbtnAgreeAll_Click(object sender, EventArgs e)
        {
            if (iselectRelation.UpdateCompanyInfoCheckStatus("", 0, true) == 1)
            {
                HZ.Web.WebUtil.Alert("操作成功！");

                BindCompanyInfo();
            }
            else
            {
                HZ.Web.WebUtil.Alert("操作失败！");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hzbtnAgree_Click(object sender, EventArgs e)
        {
            updateStatusBatch(1);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hzbtnDeny_Click(object sender, EventArgs e)
        {
            updateStatusBatch(2);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        protected void updateStatusBatch(int status)
        {
            string strCompanyID = string.Empty;
            //遍历列表的所有行
            for (int _index = 0; _index < gvwCheckList.Rows.Count; _index++)
            {
                RadioButton radb = ((RadioButton)gvwCheckList.Rows[_index].FindControl("rdobutton"));
                if (radb.Checked )
                {
                    strCompanyID = this.gvwCheckList.DataKeys[_index].Values["CompanyID"].ToString();
                    break;
                }
            }
            if (string.IsNullOrEmpty(strCompanyID))
            {
                HZ.Web.WebUtil.Alert("请至少选择一条记录！");
                return;
            }
            else
            {
                if (iselectRelation.UpdateCompanyInfoCheckStatus(strCompanyID, status, false) == 1)
                {
                    HZ.Web.WebUtil.Alert("操作成功！");

                    BindCompanyInfo();
                }
                else
                {
                    HZ.Web.WebUtil.Alert("操作失败！");
                }
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            int totalCount;
            string strWhere = "";


            if (string.IsNullOrEmpty(txtCompanyName.Text.Trim()) == false && !string.IsNullOrEmpty(HZ.Fun2.CheckString(txtCompanyName.Text.Trim())))
            {
                strWhere += " and TB_CompanyInfo.companyname like '%" + txtCompanyName.Text.Trim() + "%' ";
            }
            if (string.IsNullOrEmpty(txtCompanyid.Text.Trim()) == false && !string.IsNullOrEmpty(HZ.Fun2.CheckString(txtCompanyid.Text.Trim())))
            {
                strWhere += " and TB_CompanyInfo.companyid like '%" + txtCompanyid.Text.Trim() + "%' ";
            }
            if (ddltype.SelectedValue != "")
            {
                strWhere += " and TB_Company.companytype = '" + ddltype.SelectedValue + "'";
            }
            strWhere += "  order by TB_CompanyInfo.companyname";

            string sql = string.Format(@"SELECT TB_CompanyInfo.companyid 企业编号,tb_companyinfo.companyname 企业名称,case tb_company.companytype when 2 then '配送企业' when 3 then '生产企业直接配送' end as 企业类型,
        tb_companyinfo.telno 企业联系电话,tb_companyinfo.author 被授权人,tb_companyinfo.authorcell 被授权人手机
  FROM TB_CompanyInfo inner join TB_Company
on TB_Company.companyid=TB_CompanyInfo.companyid  
where ((companytype<>0 and companytype<>1) or TB_Company.companyid in ('J0156','J0587','J0604')) {0}", strWhere);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "企业信息.xls");
            //DataTable dt = ic.GetCompanyInfoByWhere(strWhere, 1, 0, out totalCount);
            //HZ.Excel.ExcelHelper.ExportDataForWeb(dt, gvwCheckList);
        }

    }
}
